package com.dj.mingkong.admin.biz.controller.system;

import com.dj.mingkong.admin.biz.controller.system.vo.request.DictionaryAddVo;
import com.dj.mingkong.admin.biz.controller.system.vo.request.DictionaryQueryVo;
import com.dj.mingkong.admin.biz.controller.system.vo.request.DictionaryUpdateVo;
import com.dj.mingkong.admin.biz.controller.system.vo.response.DictionaryVo;
import com.dj.mingkong.admin.biz.service.system.DictionaryService;
import com.dj.mingkong.common.annotation.HasRoles;
import com.dj.mingkong.common.enums.RoleEnum;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.util.AssertUtils;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典管理
 *
 * @author zenglonghui
 * @date: 2022/7/12 16:59
 */
@RestController
@RequestMapping("/dict")
@HasRoles(RoleEnum.ADMIN)
public class DictionaryController {
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 新增数据
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException dictionaryVo为空的时候会抛出运行时异常
     */
    @PostMapping("/add")
    public RestResponse<Boolean> insert(@RequestBody @Validated DictionaryAddVo addVo) {
        dictionaryService.insert(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 修改数据
     *
     * @param updateVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException dictionaryVo.getId()为空的时候会抛出运行时异常
     */
    @PostMapping("/updateById")
    public RestResponse<Boolean> updateById(@RequestBody @Validated DictionaryUpdateVo updateVo) {
        dictionaryService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 通过主键删除数据
     *
     * @param deleteVo -id 主键
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException id 为空的时候会抛出运行时异常
     */
    @PostMapping("/deleteById")
    public RestResponse<Boolean> deleteById(@RequestBody @Validated DeleteVo deleteVo) {
        dictionaryService.deleteById(deleteVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 查询多条数据
     *
     * @param queryVo 查询Vo queryVo - pageNum 查询第几页，如果输入是 < 1 那么会被重置成1 queryVo - pageSize
     *                查询分页大小，如果输入是 < 1 那么会被重置成1
     * @return 对象分页
     * @throws RuntimeException sysDictionaryVo为空的时候会抛出运行时异常
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<DictionaryVo>> pageByVo(@RequestBody @Validated DictionaryQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        return RestResponse.success(dictionaryService.pageByVo(queryVo));
    }

    /**
     * 根据父Key获取子列表
     *
     * @param key 参数
     * @return List<DictionaryVo>
     */
    @GetMapping("/getByParentKey/{key}")
    public RestResponse<List<DictionaryVo>> getByParentKey(@PathVariable("key") @NotBlank String key) {

        return RestResponse.success(dictionaryService.getByParentKey(key));
    }


    /**
     * 获取树形结构数据列表
     *
     * @return List<DictionaryVo>
     */
    @GetMapping("/listTree")
    public RestResponse<List<DictionaryVo>> listTree() {

        return RestResponse.success(dictionaryService.listTree());
    }
}
